<?php
session_name("interoprouter");
session_start();

/*
 * CONFIGURACAO OBRIGATORIAS
 * variaveis de configuracao obrigatorias para funcionamento do site
 */
$name = "admin";	// nome da pagina atual
$_SESSION["page"] = "admin.php";
if ( isset( $_SESSION["lang"] ) == false ) $_SESSION["lang"] = "br";
$lang = $_SESSION["lang"];


/*
 * CONFIGURACAO 
 * variaveis de configuracao
 */
$gatelevel = "admin";
$title = "Website";

/*
 * CABECALHO DA PAGINA
 * includes e inicio da construcao do layout
 */
ob_start(); include("modules/session.php"); ob_end_clean();
ob_start(); include("modules/database.php"); ob_end_clean();
ob_start(); include("template/templateBodyGen.php"); ob_end_clean();

/*
 * CONTROLES DO CASO DE USO
 * funcoes que o caso de uso vai executar
 */
//$exemplo = array("dado 1", "dado 2", "dado 3", "dado 4");

	if ( isset( $_GET["acao"] ) == false ) $acao = "show";
	else $acao = $_GET["acao"] ;
	
	if ( isset( $_GET["tabela"] ) == false ) $tabela = "accounts";
	else $tabela = $_GET["tabela"];
	
	switch ( $tabela )
	{
		case "accounts":
			switch ( $acao )
			{
				case "show":
					$dbSql = "SELECT * FROM accounts ORDER BY accountOwner";
					$dbResult = @mysql_query($dbSql);
					if ($dbResult == false)
						$flagDB = false;
					else
					{
						$flagDB = true;
						while ($dbRow = @mysql_fetch_assoc($dbResult))
						{
							$accountId = $dbRow["accountId"];
							$accountUser = $dbRow["accountUser"];
							$accountOwner = $dbRow["accountOwner"];
							$accountIsAdmin = ($dbRow["accountIsAdmin"] == 1) ? ("yes") : ("no");;
							$data[] = array("accountId" => $accountId, "accountUser" => $accountUser, "accountOwner" => $accountOwner, "accountIsAdmin" => $accountIsAdmin);
						}
						@mysql_free_result($dbResult);
					}
				break;
				case "remove":
					if ( isset( $_GET["accountId"] ) == false ) { header ("Location: admin.php?acao=show&tabela=accounts"); exit(); }
					$_accountId = $_GET["accountId"];
					$dbSql = "DELETE FROM accounts WHERE accountId=".$_accountId." LIMIT 1";
					$dbResult = @mysql_query($dbSql);
					@mysql_free_result($dbResult);
					header ("Location: admin.php?acao=show&tabela=accounts"); exit();
				break;
				case "add":
				break;
				case "insert":
					$_accountUser = $_POST["accountUser"];
					$_accountPassword = $_POST["accountPassword"];
					$_accountOwner = $_POST["accountOwner"];
					$_accountEmail = $_POST["accountEmail"];
					$_accountOrganization = $_POST["accountOrganization"];
					$_accountTelephone = $_POST["accountTelephone"];
					$dbSql = "INSERT INTO accounts (accountId, accountUser, accountPassword, accountLastLogin, accountOwner, accountEmail, accountOrganization, accountTelephone, accountIsAdmin) ";
					$dbSql .= "VALUES              (NULL, '$_accountUser', '$_accountPassword', NOW( ) , '$_accountOwner', '$_accountEmail', '$_accountOrganization', '$_accountTelephone', '0');";
					$dbResult = @mysql_query($dbSql);
					@mysql_free_result($dbResult);
					header ("Location: admin.php?acao=show&tabela=accounts"); exit();
				break;
				case "edit":
					if ( isset( $_GET["accountId"] ) == false ) { header ("Location: admin.php?acao=show&tabela=accounts"); exit(); }
					$_accountId = $_GET["accountId"];
					$dbSql = "SELECT * FROM accounts WHERE accountId=".$_accountId." LIMIT 1";
					$dbResult = @mysql_query($dbSql);
					$data = @mysql_fetch_assoc($dbResult);
					@mysql_free_result($dbResult);
				break;
				case "update":
					$_accountId = $_POST["accountId"];
					$_accountUser = $_POST["accountUser"];
					$_accountPassword = $_POST["accountPassword"];
					$_accountOwner = $_POST["accountOwner"];
					$_accountEmail = $_POST["accountEmail"];
					$_accountOrganization = $_POST["accountOrganization"];
					$_accountTelephone = $_POST["accountTelephone"];
					$dbSql = "UPDATE accounts SET accountUser='$_accountUser', accountPassword='$_accountPassword', accountOwner='$_accountOwner', ";
					$dbSql .= " accountEmail='$_accountEmail', accountOrganization='$_accountOrganization', accountTelephone='$_accountTelephone' ";
					$dbSql .= " WHERE accountId='$_accountId' LIMIT 1;";
					$dbResult = @mysql_query($dbSql);
					@mysql_free_result($dbResult);
					header ("Location: admin.php?acao=show&tabela=accounts"); exit();
				break;
				case "admin":
					if ( isset( $_GET["accountId"] ) == false ) { header ("Location: admin.php?acao=show&tabela=accounts"); exit(); }
					$_accountId = $_GET["accountId"];
					$dbSql = "SELECT accountIsAdmin FROM accounts WHERE accountId=".$_accountId." LIMIT 1";
					$dbResult = @mysql_query($dbSql);
					$dbRow = @mysql_fetch_assoc($dbResult);
					$test = ($dbRow["accountIsAdmin"] == 1) ? (0) : (1);
					@mysql_free_result($dbResult);
					$dbSql = "UPDATE accounts SET accountIsAdmin=$test WHERE accountId='$_accountId' LIMIT 1;";
					$dbResult = @mysql_query($dbSql);
					@mysql_free_result($dbResult);
					header ("Location: admin.php?acao=show&tabela=accounts"); exit();
				break;
			}
		break;
		case "clusters":
			switch ( $acao )
			{
				case "show":
					$dbSql = "SELECT * FROM clusters ORDER BY clusterName";
					$dbResult = @mysql_query($dbSql);
					if ($dbResult == false)
						$flagDB = false;
					else
					{
						$flagDB = true;
						while ($dbRow = @mysql_fetch_assoc($dbResult))
						{
							$clusterId = $dbRow["clusterId"];
							$clusterName = $dbRow["clusterName"];
							$clusterOperationalSystem = $dbRow["clusterOperationalSystem"];
							$clusterActive = ($dbRow["clusterActive"] == 1) ? ("yes") : ("no");
							$data[] = array("clusterId" => $clusterId, "clusterName" => $clusterName, "clusterOperationalSystem" => $clusterOperationalSystem, "clusterActive" => $clusterActive);
						}
						@mysql_free_result($dbResult);
					}
				break;
				case "remove":
					if ( isset( $_GET["clusterId"] ) == false ) { header ("Location: admin.php?acao=show&tabela=clusters"); exit(); }
					$_clusterId = $_GET["clusterId"];
					$dbSql = "DELETE FROM clusters WHERE clusterId=".$_clusterId." LIMIT 1";
					$dbResult = @mysql_query($dbSql);
					@mysql_free_result($dbResult);
					header ("Location: admin.php?acao=show&tabela=clusters"); exit();
				break;
				case "add":
				break;
				case "insert":
					$_clusterName = $_POST["clusterName"];
					$_clusterIP = $_POST["clusterIP"];
					$_clusterOperationalSystem = $_POST["clusterOperationalSystem"];
					$_clusterScheduler = $_POST["clusterScheduler"];
					$_clusterProcessorModel = $_POST["clusterProcessorModel"];
					$_clusterNumNode = $_POST["clusterNumNode"];
					$_clusterNumProcessorsPerNode = $_POST["clusterNumProcessorsPerNode"];
					$dbSql = "INSERT INTO clusters (clusterId, clusterName, clusterIP, clusterOperationalSystem, clusterScheduler, clusterProcessorModel, clusterNumNode, clusterNumProcessorsPerNode, clusterActive, clusterTimestampAlive, clusterWorkLoad) ";
					$dbSql .= "VALUES              (NULL, '$_clusterName', '$_clusterIP', '$_clusterOperationalSystem', '$_clusterScheduler', '$_clusterProcessorModel', '$_clusterNumNode', '$_clusterNumProcessorsPerNode', '0', NOW(), 0);";
					$dbResult = @mysql_query($dbSql);
					@mysql_free_result($dbResult);
					header ("Location: admin.php?acao=show&tabela=clusters"); exit();
				break;
				case "edit":
					if ( isset( $_GET["clusterId"] ) == false ) { header ("Location: admin.php?acao=show&tabela=clusters"); exit(); }
					$_clusterId = $_GET["clusterId"];
					$dbSql = "SELECT * FROM clusters WHERE clusterId=".$_clusterId." LIMIT 1";
					$dbResult = @mysql_query($dbSql);
					$data = @mysql_fetch_assoc($dbResult);
					@mysql_free_result($dbResult);
				break;
				case "update":
					$_clusterId = $_POST["clusterId"];
					$_clusterName = $_POST["clusterName"];
					$_clusterIP = $_POST["clusterIP"];
					$_clusterOperationalSystem = $_POST["clusterOperationalSystem"];
					$_clusterScheduler = $_POST["clusterScheduler"];
					$_clusterProcessorModel = $_POST["clusterProcessorModel"];
					$_clusterNumNode = $_POST["clusterNumNode"];
					$_clusterNumProcessorsPerNode = $_POST["clusterNumProcessorsPerNode"];
					$dbSql = "UPDATE clusters SET clusterName='$_clusterName', clusterIP='$_clusterIP', ";
					$dbSql .= " clusterOperationalSystem='$_clusterOperationalSystem', clusterScheduler='$_clusterScheduler', ";
					$dbSql .= " clusterProcessorModel='$_clusterProcessorModel', clusterNumNode='$_clusterNumNode', ";
					$dbSql .= " clusterNumProcessorsPerNode='$_clusterNumProcessorsPerNode' ";
					$dbSql .= " WHERE clusterId='$_clusterId' LIMIT 1;";
					$dbResult = @mysql_query($dbSql);
					@mysql_free_result($dbResult);
					header ("Location: admin.php?acao=show&tabela=clusters"); exit();
				break;
				case "active":
					if ( isset( $_GET["clusterId"] ) == false ) { header ("Location: admin.php?acao=show&tabela=clusters"); exit(); }
					$_clusterId = $_GET["clusterId"];
					$dbSql = "SELECT clusterActive FROM clusters WHERE clusterId=".$_clusterId." LIMIT 1";
					$dbResult = @mysql_query($dbSql);
					$dbRow = @mysql_fetch_assoc($dbResult);
					$test = ($dbRow["clusterActive"] == 1) ? (0) : (1);
					@mysql_free_result($dbResult);
					$dbSql = "UPDATE clusters SET clusterActive=$test WHERE clusterId='$_clusterId' LIMIT 1;";
					$dbResult = @mysql_query($dbSql);
					@mysql_free_result($dbResult);
					header ("Location: admin.php?acao=show&tabela=clusters"); exit();
				break;
			}
		break;
	}

/*
 * RENDERIZA PAGINA
 * aqui vao os includes e as variaveis de configuracao
 */
$javascripts = array();
makeHeader($title, $javascripts);
// $flagDB # database on or off
// $data # all data
include("lang/{$lang}/{$name}.php");



/*
 * RODAPE DA PAGINA
 * encerra a pagina coma as tags necessarias
 */
makeFooter();

?>